/*
 * ActualAdmin.java
 *
 * Created on 25 de julio de 2007, 11:26 AM
 */

package administrador;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import javax.swing.*;
/**
 *
 * @author  daniel1
 */
public class ActualAdmin extends javax.swing.JFrame {
  Conexion conexion;
  int bodega;
  Usuario user;
    /** Creates new form ActualAdmin */
    public ActualAdmin(Conexion cEnt,int bEnt,Usuario user) {
        conexion=cEnt;
        bodega=bEnt;
        this.user=user;
        
        initComponents();
        setVisible(true);
        setSize(530,345);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txt_bdOrigen = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_bdDestino = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        cb_opAct = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cb_encero = new javax.swing.JCheckBox();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Actializacion Menu"));
        jPanel1.setLayout(null);

        jLabel1.setFont(new java.awt.Font("SansSerif", 1, 24));
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setText("Actualizacion");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 20, 220, 40);

        jLabel14.setForeground(new java.awt.Color(255, 153, 0));
        jLabel14.setText("Menu");
        jPanel1.add(jLabel14);
        jLabel14.setBounds(160, 40, 70, 40);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("1.- Definir Origenes de Datos"));
        jPanel2.setLayout(null);

        jLabel2.setText("Origen:");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(130, 30, 36, 14);

        txt_bdOrigen.setText("sexylocurasloc2");
        jPanel2.add(txt_bdOrigen);
        txt_bdOrigen.setBounds(170, 30, 190, 20);

        jLabel3.setText("Destino");
        jPanel2.add(jLabel3);
        jLabel3.setBounds(130, 60, 36, 14);

        txt_bdDestino.setText("bdmultiadmin");
        jPanel2.add(txt_bdDestino);
        txt_bdDestino.setBounds(170, 60, 190, 20);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Base de Datos:");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(20, 20, 91, 15);

        jLabel6.setText("MYSQL:");
        jPanel2.add(jLabel6);
        jLabel6.setBounds(70, 40, 37, 14);

        jPanel1.add(jPanel2);
        jPanel2.setBounds(10, 80, 460, 100);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Proceso de Actualizacion"));

        jButton1.setText("Actualizar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        cb_opAct.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sumar", "Restar", "Reemplazar" }));
        cb_opAct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_opActActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("Tipo de Actualizacion:");

        cb_encero.setFont(new java.awt.Font("Tahoma", 1, 11));
        cb_encero.setText("Encero Cant.");
        cb_encero.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cb_encero.setMargin(new java.awt.Insets(0, 0, 0, 0));

        jButton2.setText("Actualizar Version");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(cb_encero)
                        .addComponent(cb_opAct, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(79, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(5, 5, 5)
                        .addComponent(cb_opAct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cb_encero)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel1.add(jPanel3);
        jPanel3.setBounds(10, 180, 460, 120);

        jButton3.setText("RefrescarBodega");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(333, 33, 130, 40);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cb_opActActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_opActActionPerformed
// TODO add your handling code here:
       //System.out.println("El valor a  guardar es: |"+cb_opAct.getSelectedIndex()+"|");
        if(cb_opAct.getSelectedIndex()==2){
        cb_encero.setVisible(true);
        }else{
        cb_encero.setVisible(false);
        }
    }//GEN-LAST:event_cb_opActActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
        //Valido que llenen todos los datos:
        if(txt_bdOrigen.getText().equals("")||txt_bdDestino.getText().equals("")) {
            JOptionPane.showMessageDialog(this,"Los datos no han sido llenados correctamente","Informacion",JOptionPane.INFORMATION_MESSAGE);
        }else{
            if(cb_opAct.getSelectedIndex()>=0){
            ActualizarBodega ab=new ActualizarBodega(txt_bdOrigen.getText(),txt_bdDestino.getText(),cb_opAct.getSelectedIndex(),cb_encero.isSelected());
            
            }
            else{
            JOptionPane.showMessageDialog(this,"La Opcion de Actualizacion a sido especificada","Informacion",JOptionPane.INFORMATION_MESSAGE);
            }
                
        }
        
        
    }//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    // TODO add your handling code here:
        //Valido que llenen todos los datos:
        if(txt_bdOrigen.getText().equals("")||txt_bdDestino.getText().equals("")) {
            JOptionPane.showMessageDialog(this,"Los datos no han sido llenados correctamente","Informacion",JOptionPane.INFORMATION_MESSAGE);
        }else{
            if(cb_opAct.getSelectedIndex()>=0){
            ActualizarVersion ab=new ActualizarVersion(txt_bdOrigen.getText(),txt_bdDestino.getText(),cb_opAct.getSelectedIndex(),cb_encero.isSelected(),user,conexion);
            
            }
            else{
            JOptionPane.showMessageDialog(this,"La Opcion de Actualizacion a sido especificada","Informacion",JOptionPane.INFORMATION_MESSAGE);
            }
                
        }
        
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

    // TODO add your handling code here:
    ResultSet r = conexion.consultarBase("SELECT cod_producto,cantidad FROM itm_bodega;");
    int filaTot=conexion.obtenerFilas(r);
    int contador=0;
    try {
        r.beforeFirst();


        while (r.next()) {
            String codigo = r.getString(1);
            Integer cantidad = r.getInt(2);
            boolean exito=false;
            exito=conexion.actualizarBaseResp("update producto set stock_total="+cantidad+" where cod_producto like '"+codigo+"';");
            if(exito==true){
                contador++;
            }
        }
    } catch (SQLException ex) {
        
    }
    if(contador!=filaTot){
        JOptionPane.showMessageDialog(this,"No se actualizo todos los productos itm bodega con sus productos","Informacion",JOptionPane.INFORMATION_MESSAGE);
    }else{
        JOptionPane.showMessageDialog(this,"Actualizacion Exitosa "+contador+"/"+filaTot+" ","Informacion",JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_jButton3ActionPerformed
    
   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox cb_encero;
    private javax.swing.JComboBox cb_opAct;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txt_bdDestino;
    private javax.swing.JTextField txt_bdOrigen;
    // End of variables declaration//GEN-END:variables
    
}
